package com.magicborrow.views;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.magicborrow.BaseLog;
import com.magicborrow.R;

import java.util.ArrayList;

/**
 * Created by River on 3/29/16.
 */
public abstract class BaseRecyclerViewLoadMoreAdapter extends RecyclerView.Adapter {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_NORMAL = 2;
    private ArrayList mDatas = new ArrayList();

    private View mFootView;
    private boolean hasFooter = false;

    protected void setData(ArrayList pData) {
        this.mDatas = pData;
    }

    // 默认是没有加载更多，设置为true则拥有默认的footview
    public void setHasFooter(boolean pHasFooter) {
        this.hasFooter = pHasFooter;
    }

    public void setFooterView(View pView) {
        this.mFootView = pView;
        hasFooter = true;
    }

    @Override
    public int getItemViewType(int position) {
        if (mFootView != null) {
            return TYPE_FOOTER;
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View footView = LayoutInflater.from(parent.getContext()).inflate(R.layout.load_more_footer, null);
            return new FootViewHolder(footView);
        }
        return buildViewHolder(parent, viewType);
    }

    public abstract RecyclerView.ViewHolder buildViewHolder(ViewGroup parent, int viewType);

    public abstract void bindViewHolderItem(RecyclerView.ViewHolder holder, int position);

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FootViewHolder) {
            holder.itemView.setTag("foot");
            return;
        } else {
            bindViewHolderItem(holder, position);
        }
    }


    @Override
    public int getItemCount() {
        int count = mDatas.size();
        if (hasFooter) {
            count++;
        }
        return count;
    }

    public class FootViewHolder extends RecyclerView.ViewHolder {

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
